using UnityEngine;

public class FacebookLogin : MonoBehaviour
{
	public void OnClickToLogin()
	{
		if (Application.platform == RuntimePlatform.OSXEditor)
		{
			FakeLogin();
		}
		else if (!GGFacebook.instance.IsAvailable())
		{
			TableTennisDialog.instance.ShowOk("Facebook Login Unavailable", "Facebook login is currently unavailable. Sorry :(", "Ok", delegate
			{
				NavigationManager.instance.Pop();
			});
		}
		else
		{
			TableTennisDialog.instance.ShowYesNo("Login to Facebook", "Would you like to log in to Facebook?", "Login", "Cancel", OnLoginDialogComplete);
			Analytics.instance.FacebookDialogOpen();
		}
	}

	public void FakeLogin()
	{
		Debug.Log("FakeLogin");
		FacebookStatusChanged(true);
	}

	public void OnLoginDialogComplete(bool success)
	{
		if (success)
		{
			LogIn();
		}
		NavigationManager.instance.Pop();
	}

	private void LogIn()
	{
		GGFacebook instance = GGFacebook.instance;
		instance.onStatusChanged -= FacebookStatusChanged;
		instance.onStatusChanged += FacebookStatusChanged;
		instance.OnTryConnect();
		Analytics.instance.FacebookLogin();
	}

	public void FacebookStatusChanged(bool connected)
	{
		if (connected)
		{
			Debug.Log(" login test start ");
			TableTennisMainNavigation tableTennisMainNavigation = NavigationManager.instance as TableTennisMainNavigation;
			if (tableTennisMainNavigation != null)
			{
				BehaviourSingletonInit<SnapshotSyncManager>.instance.OnFacebookLogin();
			}
			BehaviourSingletonInit<GGServerRequestsBackend>.instance.StartCoroutine(BehaviourSingletonInit<GGServerRequestsBackend>.instance.GetFacebookLogin(AuthenticateToGGServer));
			Analytics.instance.FacebookLoginSuccess();
		}
		else
		{
			TableTennisDialog.instance.ShowOk("Couldn't connect to Facebook.", "Couldn't connect to Facebook. Please check your connection and try again.", "Ok", null);
			Analytics.instance.FacebookLoginFailded();
		}
	}

	public void AuthenticateToGGServer(GGServerRequestsBackend.ServerRequest request)
	{
		Debug.Log("AuthenticateToGGServer");
		if (request.status != 0)
		{
			Debug.Log("Error! " + request.status);
			TableTennisMainNavigation tableTennisMainNavigation = NavigationManager.instance as TableTennisMainNavigation;
			if (tableTennisMainNavigation != null)
			{
				BehaviourSingletonInit<SnapshotSyncManager>.instance.OnFacebookLoginError(request.errorMessage);
			}
		}
		else
		{
			GGPlayerSettings.instance.Model.hasLoggedIntoFacebook = true;
			GGPlayerSettings.instance.Model.canCloudSync = true;
			GGPlayerSettings.instance.Save();
			BehaviourSingletonInit<FriendProfilesManager>.instance.RefreshFriendProfiles();
			SyncWithCloudIfAvailable();
		}
	}

	private void SyncWithCloudIfAvailable()
	{
		TableTennisMainNavigation tableTennisMainNavigation = NavigationManager.instance as TableTennisMainNavigation;
		switch ((int)ConfigBase.instance.cloudSyncType)
		{
		case 1:
		{
			Debug.Log("GGCloudSyncFileIOSync");
			GGCloudSyncFileIOSync gGCloudSyncFileIOSync = GGFileIOCloudSync.instance as GGCloudSyncFileIOSync;
			gGCloudSyncFileIOSync.SyncWithDelay();
			if (tableTennisMainNavigation != null && !BehaviourSingletonInit<SnapshotSyncManager>.instance.syncInProgress)
			{
				BehaviourSingletonInit<SnapshotSyncManager>.instance.Init();
				BehaviourSingletonInit<SnapshotSyncManager>.instance.DoInitialSync();
			}
			break;
		}
		case 3:
			Debug.Log("GGSnapshotCloudSync");
			if (tableTennisMainNavigation != null && !BehaviourSingletonInit<SnapshotSyncManager>.instance.syncInProgress)
			{
				BehaviourSingletonInit<SnapshotSyncManager>.instance.Init();
				BehaviourSingletonInit<SnapshotSyncManager>.instance.DoInitialSync();
			}
			break;
		default:
			Debug.Log("no sync available");
			if (tableTennisMainNavigation != null && !BehaviourSingletonInit<SnapshotSyncManager>.instance.syncInProgress)
			{
				BehaviourSingletonInit<SnapshotSyncManager>.instance.Init();
				BehaviourSingletonInit<SnapshotSyncManager>.instance.DoInitialSync();
			}
			break;
		}
	}
}
